package phylogenetictree;

import java.util.ArrayList;

import javax.swing.JFrame;

public class Forest {
	private PhylogeneticTree tree;
	private ArrayList<Integer> representives;
	private ArrayList<Integer> AllRepresentives;
	
	public Forest(PhylogeneticTree tree)
	{
		this.tree = tree;
		Integer leaf = tree.getleaf();
		
		this.representives= new ArrayList<Integer>();
		this.AllRepresentives= new ArrayList<Integer>();
		
		this.representives.add(leaf);
		this.AllRepresentives.add(leaf);
	}
	
	public Integer getTreeRepsentitive(int index)
	{
		return this.representives.get(index);
	}
	
	public Integer getTreeRepsentitiveSize()
	{
		return this.representives.size();
	}
	
	public ArrayList<Integer> getTreeRepsentitiveAll()
	{
		return this.AllRepresentives;
	}
	
	public Integer getTreeRepsentitiveAll(int index)
	{
		return this.AllRepresentives.get(index);
	}
	
	public Integer getTreeRepsentitiveAllSize()
	{
		return this.AllRepresentives.size();
	}
	
	public Integer removeTreeRepsentitive(int index)
	{
		return this.representives.remove(index);
	}
	
	public PhylogeneticTree getTree() {
		return tree;
	}
	
	public void removeEdge(Integer a,Integer b)
	{
		if(!this.tree.removeEdge(a, b))
		{
			System.out.println(a + ","+ b + "Cant be removed");
		}
		
//		System.out.println(tree.indexMap.get(a) +" - " + tree.indexMap.get(b) +" cut");
//		
//		JFrame dialog = new JFrame("tree one");
//		dialog.setContentPane(GUI.Example.treeVeiwer(this.tree));
//		dialog.setVisible(true);
		
		Boolean represented = false;
		for(Integer i:representives)
		{
			if(PhylogeneticTree.connected(tree, a, -1, i))
			{
				represented = true;
				break;
			}
		}
		
		if(!represented)
		{
			a = PhylogeneticTree.getleaf(tree, a, -1);
			this.representives.add(a);
			this.AllRepresentives.add(a);
		}
		else
		{
			b = PhylogeneticTree.getleaf(tree, b, -1);
			this.AllRepresentives.add(b);
			this.representives.add(b);
		}
		
		tree.removepointlessVertexs();
		
//		JFrame dialogTwo = new JFrame("tree one");
//		dialog.setContentPane(GUI.Example.treeVeiwer(this.tree));
//		dialog.setVisible(true);
//		
//		System.out.println("hi");
	}
}
